<template>
	<div>
		<van-nav-bar title="闲置订单"></van-nav-bar>
		<div>
			<van-pull-refresh v-model="flashing" @refresh="flashOrder"
				style="overflow: scroll;height: calc(100vh - 46px) ;">
				<van-list v-model:loading="loading" :finished="noMore" finished-text="没有更多了" @load="pushOrder">
					<van-cell v-for="(order,key) in dataList" :key="key">
						<GoodOrderCell :order="order" :showPayWay="pay"></GoodOrderCell>
					</van-cell>
				</van-list>
			</van-pull-refresh>
		</div>
		<van-action-sheet v-model:show="payWayShow" :actions="payWays" cancel-text="取消" close-on-click-action
			description="请选择支付方式" @select="payWaySelect" />
		<van-popup v-model:show="payPageShow" position="right" :style="{ width: '100%',height: '100%'}" closeable
			close-icon-position="top-left">
			<PayPage :price='priceShow' userName='锦江帮帮' :close="payPageClose" :url="resurl"
				:data="orderOffers"></PayPage>
		</van-popup>
	</div>
</template>

<script>
	import {
		showNotify,
		showLoadingToast
	} from 'vant';
	import url from '@/assets/url.json';
	import GoodOrderCell from '@/components/units/GoodOrderCell.vue';
	import PayPage from '@/components/pages/PayPage.vue';

	export default {


		props: {
			uid: String
		},
		components: {
			GoodOrderCell,
			PayPage
		},

		data() {
			return {
				noMore: false,
				flashing: false,
				loading: false,
				dataList: [],
				page: 1,
				limit: 5,
				orderOffers: '',
				urlIp: url.ip,
				payWays: [{
						name: '微信支付',
						label: 'wechat'
					},
					{
						name: '钱包支付',
						label: 'wallet'
					}
				],
				payWayShow: false,
				payPageShow: false,
				priceShow: '',
				resurl: url.ip + '/api-goods/orderGoods/payOrder',
			}
		},

		methods: {
			payWaySelect(payName) {
				switch (payName.label) {
					case 'wechat':
						showLoadingToast({
							message: '加载中...',
							forbidClick: true,
						});
						setTimeout(() => {
							this.payPageShow = true
						}, 2000);
						break;
					case 'wallet':
						showLoadingToast({
							message: '支付中...',
							forbidClick: true,
						});
						setTimeout(() => {
							// this.$axios({
							// 	url: url.ip + "/api-offer/order/payFromWallet?oid=" + this.orderOffers.id,
							// 	method: 'GET',
							// 	headers: {
							// 		token: this.$cookies.get('token')
							// 	}
							// }).then((res) => {
							// 	if (res.data.code !== 0) {
							// 		showNotify({
							// 			type: 'danger',
							// 			message: res.data.message
							// 		});
							// 		this.close();
							// 	} else {
							// 		showNotify({
							// 			type: 'success',
							// 			message: "支付成功"
							// 		});
							// 	}
							// })
							showNotify({
								type: 'danger',
								message: "暂不支持钱包支付"
							});
						}, 2000);

						break;
				}
			},



			pay(priceShow, oid,user) {
				this.$axios({
					url: url.ip + '/api-user/user',
					method: 'GET',
					headers: {
						token: this.$cookies.get('token'),
					}
				}).then((res) => {
					if (res.data.code === 0) {
						this.priceShow = priceShow;
						this.orderOffers = {
							money: this.priceShow,
							user: user,
							id: oid
						};
						this.payWayShow = true;
					}
				}).catch((err) => {
					console.log(err);
				})
			},


			flashOrder() {
				this.page = 1;
				this.noMore = false;
				this.dataList = [];
				this.pushOrder();
			},

			load() {
				this.pushOrder();
			},

			pushOrder() {
				this.$axios({
					url: url.ip + '/api-goods' + '/orderGoods/getOrderByUid',
					method: 'GET',
					params: {
						page: this.page,
						limit: this.limit,
						id: this.uid
					},
					headers: {
						token: this.$cookies.get('token'),
					}
				}).then((res) => {
					if (res.data.code !== 0) {
						this.noMore = true;
						showNotify({
							type: 'danger',
							message: res.data.message
						});
					} else {
						let list = res.data.data.entityList;
						if (list.length <= 0) {
							this.noMore = true;
						} else {
							for (let key in list) {
								this.dataList.push(list[key]);
							}
						}
					}
					this.page += 1;
					this.loading = false;
					this.flashing = false;
				}).catch(() => {
					this.loading = false;
					this.flashing = false;
					this.noMore = true;
					showNotify({
						type: 'danger',
						message: "网络错误或未登录"
					});
				})
			},
			payPageClose(){
				this.payPageShow=false;
			}
		}
	}
</script>

<style>
</style>